package com.car.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.car.entity.User;
import com.car.page.Page;
import com.car.service.VehiclemotioncarService;
import com.car.vo.CarrentalorderformVo;
import com.car.vo.VehiclemotioncarVo;

@Controller
@RequestMapping("/shun")
public class VehiclemotioncarController {
	
	@Autowired
	VehiclemotioncarService vehiclemotioncarService;
	
	/**
	 * 顺风车列表
	    * @Title: show
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @param city
	    * @param @param takeCarCity
	    * @param @param carCity
	    * @param @param qing
	    * @param @param pageNo
	    * @param @param pageSize
	    * @param @param model
	    * @param @param httpSession
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	@RequestMapping("/toViewShow")
	public String  show(@RequestParam(value="City",required=false) String city,
			@RequestParam(value="takeCarCity",required=false) String takeCarCity,
			@RequestParam(value="carCity",required=false) String carCity,
			@RequestParam(value="qing",required=false) String qing,
			@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = false, defaultValue = "1") Integer pageSize,
			Model model,HttpSession httpSession){
		
		Page<VehiclemotioncarVo> page = vehiclemotioncarService.selectAllShunCar(takeCarCity, carCity, pageNo, pageSize);
		
		for (int i = 0; i < page.getList().size(); i++) {
			page.getList().get(i).setTheRent(page.getList().get(i).getTheRent()/2*page.getList().get(i).getDays());
		}
		model.addAttribute("page", page);
		model.addAttribute("vehList", page.getList());
		model.addAttribute("pageIndex", pageNo);
		if(qing!=null){
			httpSession.removeAttribute("lai");
			httpSession.removeAttribute("qu");
		}
		
		if(takeCarCity!=null){
			httpSession.setAttribute("lai", takeCarCity);
		}
		if(carCity!=null){
			httpSession.setAttribute("qu", carCity);
		}
		if(city!=null){
			httpSession.setAttribute("cityTo", city);
		}
		return "H5phone/vehiclemotionshow";
	}
	
	/**
	 * 取车
	    * @Title: tocitys
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @param city
	    * @param @param model
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	@RequestMapping("/tocitys")
	public String  tocitys(@RequestParam("City") String city,Model model){
		model.addAttribute("city", city);
		model.addAttribute("fan", 1);
		return "H5phone/city";
	}
	/**
	 * 还车
	    * @Title: tocitysTwo
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @param city
	    * @param @param model
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	@RequestMapping("/tocityTwo")
	public String  tocitysTwo(@RequestParam("City") String city,Model model){
		model.addAttribute("city", city);
		model.addAttribute("fan", 2);
		return "H5phone/city";
	}
	/**
	 * 订单介绍
	    * @Title: toInfos
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	@RequestMapping("/toInfos")
	public String  toInfos(){
	
		return "H5phone/freeridingrules";
	}
	/**
	 * 顺丰车详情
	    * @Title: ShunInfos
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	@RequestMapping("/orderInfos")
	public String  ShunInfos(@RequestParam("id") String vehId,@RequestParam("money") String money,
			@RequestParam(value="hccity",required=false) String hccity,
			@RequestParam(value="city",required=false) String city,
			@RequestParam(value="pageIndex",required=false) String pageIndex
			,Model model,HttpSession session){
		User user = (User) session.getAttribute("loginSuccess");
		if(user==null){
			model.addAttribute("error", "1");
		}
		VehiclemotioncarVo vehiclemotioncarVo = vehiclemotioncarService.selectAllShunCarInfos(vehId);
		model.addAttribute("id", vehId);
		model.addAttribute("money", money);
		model.addAttribute("veh", vehiclemotioncarVo);
		model.addAttribute("hccity", hccity);
		model.addAttribute("ccct", city);
		model.addAttribute("pageIndex", pageIndex);
		return "H5phone/reservation";
	}
	
	@RequestMapping("/confirmOrder")
	public String confirmOrders(
			@RequestParam("quaddress") String quaddress,
			@RequestParam("appDateTime") String appDateTime,
			@RequestParam("huanche") String huanche,
			@RequestParam("ids") Integer ids,
			@RequestParam("hccity") String hccity,
			@RequestParam("money") String money,
			@RequestParam("pageIndex") Integer pageIndex,
			Model model,HttpSession session){
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		 try {
			Map<String, Object> maps = new HashMap<>();
			maps.put("quaddress", quaddress);
			maps.put("appDateTime",sdf.parse(appDateTime));
			maps.put("huanche",sdf.parse(huanche));
			VehiclemotioncarVo vehiclemotioncarVo = vehiclemotioncarService.selectById(ids);
			vehiclemotioncarVo.setTheRent(vehiclemotioncarVo.getTheRent()/2*vehiclemotioncarVo.getDays());
		session.setAttribute("orderShunStart", vehiclemotioncarVo);
			model.addAttribute("veh", vehiclemotioncarVo);
			model.addAttribute("maps", maps);
			model.addAttribute("id", ids);
			model.addAttribute("pageIndex", pageIndex);
			model.addAttribute("money", money);
			model.addAttribute("hccity", hccity);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "H5phone/confirmOrder";
	}
	
	
	@RequestMapping("/paythepremise")
	public String paythepremise(){
		return "";
	}
	
	
}
